package com.xiaoge.biz.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoge.auth.annotation.ControllerEndpoint;
import com.xiaoge.biz.entity.Consumer;
import com.xiaoge.biz.entity.Supplier;
import com.xiaoge.biz.service.ConsumerService;
import com.xiaoge.biz.vo.SupplierVo;
import com.xiaoge.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xiaoge
 * @since 2021-03-21
 */
@RestController
@CrossOrigin
@RequestMapping("/consumer")
public class ConsumerController {
    @Autowired
    private ConsumerService consumerService;

    /**
     * 分页查询物资去处
     *
     * @param current
     * @param size
     * @param supplierVo
     * @return
     */
    @ApiOperation(value = "分页查询物资去处")
    @PostMapping("/findConsumerList")
    public Result findConsumerList(@RequestParam(defaultValue = "1") Integer current,
                                   @RequestParam(defaultValue = "6") Integer size,
                                   @RequestBody SupplierVo supplierVo) {
        //取得分页条件
        Page<Consumer> page = new Page<>(current, size);
        //获得wragger
        QueryWrapper<Consumer> queryWrapper = getWrapper(supplierVo);

        //执行查询
        IPage<Consumer> productPage = consumerService.page(page, queryWrapper);

        long total = productPage.getTotal();
        List<Consumer> records = productPage.getRecords();
        return Result.ok().data("total", total).data("records", records);
    }

    /**
     * 获取所有去处
     *
     * @param
     * @return
     */
    @ApiOperation(value = "获取所有去处")
    @GetMapping("/getAllConsumerList")
    public Result getAllConsumerList() {
        List<Consumer> consumers = consumerService.list();
        return Result.ok().data("records", consumers);
    }

    /**
     * 添加去处
     *
     * @param consumer
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "添加去处失败", operation = "添加去处")
    @ApiOperation(value = "添加去处")
    @PostMapping("/addConsumer")
    public Result addConsumer(@RequestBody Consumer consumer) {
        consumerService.save(consumer);
        return Result.ok();
    }

    /**
     * 修改去处
     *
     * @param consumer
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "修改去处失败", operation = "修改去处")
    @ApiOperation(value = "修改去处")
    @PostMapping("/updateConsumer")
    public Result updateConsumer(@RequestBody Consumer consumer) {
        consumerService.updateById(consumer);
        return Result.ok();
    }

    /**
     * 删除去处
     *
     * @param id
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "删除去处失败", operation = "删除去处")
    @ApiOperation(value = "删除去处")
    @DeleteMapping("/deleteConsumerById")
    public Result deleteConsumerById(String id) {
        consumerService.removeById(id);
        return Result.ok();
    }

    private QueryWrapper<Consumer> getWrapper(SupplierVo supplierVo) {
        QueryWrapper<Consumer> queryWrapper = new QueryWrapper<>();
        if (supplierVo != null) {

            //查询每个条件
            if (!ObjectUtils.isEmpty(supplierVo.getName())) {
                queryWrapper.like("name", supplierVo.getName());
            }
            if (!ObjectUtils.isEmpty(supplierVo.getAddress())) {
                queryWrapper.like("address", supplierVo.getAddress());
            }
            if (!ObjectUtils.isEmpty(supplierVo.getContact())) {
                queryWrapper.like("contact", supplierVo.getContact());
            }
        }
        return queryWrapper;
    }
}
